﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using System.ComponentModel;
using System.Windows;

namespace Common.Utility
{
   [TypeConverter(typeof(WindowSizeSettingsConverter))]
   [SettingsSerializeAs(SettingsSerializeAs.String)]
   public class WindowSizeSettings : ObservableObject
   {
      public WindowSizeSettings()
      {

      }

      private WindowState _State = WindowState.Normal;

      /// <summary>
      /// Sets and gets the State property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public WindowState State
      {
         get
         {
            return _State;
         }

         set
         {
            if (_State == value)
            {
               return;
            }

            _State = value;
            RaisePropertyChanged("State");
         }
      }

      /// <summary>
      /// The <see cref="Left" /> property's name.
      /// </summary>
      public const string LeftPropertyName = "Left";

      private double _Left = 0.0;

      /// <summary>
      /// Sets and gets the Left property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public double Left
      {
         get
         {
            return _Left;
         }

         set
         {
            if (_Left == value)
            {
               return;
            }

            _Left = value;
            RaisePropertyChanged(LeftPropertyName);
         }
      }

      /// <summary>
      /// The <see cref="Top" /> property's name.
      /// </summary>
      public const string TopPropertyName = "Top";

      private double _Top = 0.0;

      /// <summary>
      /// Sets and gets the Top property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public double Top
      {
         get
         {
            return _Top;
         }

         set
         {
            if (_Top == value)
            {
               return;
            }

            _Top = value;
            RaisePropertyChanged(TopPropertyName);
         }
      }

      /// <summary>
      /// The <see cref="Width" /> property's name.
      /// </summary>
      public const string WidthPropertyName = "Width";

      private double _Width = 0.0;

      /// <summary>
      /// Sets and gets the Width property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public double Width
      {
         get
         {
            return _Width;
         }

         set
         {
            if (_Width == value)
            {
               return;
            }

            _Width = value;
            RaisePropertyChanged(WidthPropertyName);
         }
      }

      /// <summary>
      /// The <see cref="Height" /> property's name.
      /// </summary>
      public const string HeightPropertyName = "Height";

      private double _Height = 0.0;

      /// <summary>
      /// Sets and gets the Height property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public double Height
      {
         get
         {
            return _Height;
         }

         set
         {
            if (_Height == value)
            {
               return;
            }

            _Height = value;
            RaisePropertyChanged(HeightPropertyName);
         }
      }
   }

   public class WindowSizeSettingsConverter : TypeConverter
   {
      public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
      {
         return sourceType == typeof(string);
      }

      public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
      {
         if (value is string)
         {
            WindowSizeSettings window = new WindowSizeSettings();

            string windowStr = value as string;

            if (String.IsNullOrWhiteSpace(windowStr))
               return window;

            string[] parts = ((string)value).Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);

            if (parts.Length != 5)
            {
               throw new ArgumentException("Must have 5 parts", "value");
            }

            window.State = (WindowState)Enum.Parse(typeof(WindowState), parts[0], true);
            window.Left = Convert.ToInt32(parts[1]);
            window.Top = Convert.ToInt32(parts[2]);
            window.Width = Convert.ToInt32(parts[3]);
            window.Height = Convert.ToInt32(parts[4]);


            return window;
         }
         return base.ConvertFrom(context, culture, value);
      }

      public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
      {
         return base.CanConvertTo(context, destinationType);
      }

      public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
      {
         if (destinationType == typeof(string))
         {
            WindowSizeSettings window = value as WindowSizeSettings;
            return string.Format("{0},{1},{2},{3},{4}", window.State, window.Left, window.Top, window.Width, window.Height);
         }
         return base.ConvertTo(context, culture, value, destinationType);
      }

   }
}
